export const getImgSrc = (richtext, num=3) => {
	let imgList = []
	richtext.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/g, (match, capture) => {
		imgList.push(capture)
	})
	imgList = imgList.slice(0, num)
	return imgList
}

export const getProvince = () => {
	return new Promise((resolve, reject) => {
		let historyProvince = uni.getStorageSync('historyProvince')
		if (historyProvince) {
			if ((Date.now() - historyProvince.time) > 1000*60*60) {
				getIp().then(res => {
					resolve(res)
				}).catch(err => {
					reject(err)
				})
				return
			}
			resolve(historyProvince.province)
			return
		}
		getIp().then(res => {
			resolve(res)
		}).catch(err => {
			reject(err)
		})
	})
}

const getIp = () => {
	return new Promise((resolve, reject) => {
		uni.request({
			url: 'https://restapi.amap.com/v3/ip?key=b728f8d4ca590f3d75e68a27f0478c44',
			success: res => {
				let str = ''
				typeof(res.data.province) == 'string' ? str = res.data.province : str = '火星'
				resolve(str)
				let obj = {
					province: str,
					time: Date.now()
				}
				uni.setStorageSync('historyProvince', obj)
			},
			fail: err => {
				reject(err)
			}
		})
	})
}
